/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.navigator;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.vaadin.addon.touchkit.ui.NavigationBar;
import com.vaadin.addon.touchkit.ui.NavigationButton;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.addon.touchkit.ui.VerticalComponentGroup;
import org.processbase.touchprocess.ui.TouchProcessApplication;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import java.util.Collection;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition.ProcessState;
import org.ow2.bonita.facade.runtime.ActivityState;
import org.ow2.bonita.light.LightProcessDefinition;
import org.ow2.bonita.light.LightTaskInstance;

public final class MainNavigationView extends NavigationView implements Button.ClickListener {

    private static final long serialVersionUID = 1L;
    private Resource iconNewCase = new ThemeResource("images/folder_add.png");
    private Resource iconTaskList = new ThemeResource("images/folder_email.png");
    private Resource iconTaskArchive = new ThemeResource("images/folder_sent.png");
    private Resource iconDashboard = new ThemeResource("images/chart_pie.png");
    CssLayout root = new CssLayout();
    private VerticalComponentGroup menuComponentGroup = null;
    private NavigationButton btnNewCase = null;
    private NavigationButton btnTaskList = null;
    private NavigationButton btnTaskArchive = null;
    private NavigationButton btnDashboard = null;
    private Button btnRefresh = null;
    private NavigationBar toolbar = null;
    static Resource refreshIcon = new ThemeResource("images/sub_blue_rotate_ccw.png");

    public MainNavigationView(final MenuNavigationManager nav) {

        setCaption("TouchProcess");
        setWidth("100%");
        setHeight("100%");

        root.setWidth("100%");
        root.setMargin(true);

        menuComponentGroup = new VerticalComponentGroup();
        Label header = new Label("Select");
        header.setSizeUndefined();
        header.addStyleName("grey-title");
        root.addComponent(header);

        prepareMenuComponentGroup();

        refreshCounts();
        root.addComponent(menuComponentGroup);
        setContent(root);
        prepareToolbar();
        setToolbar(toolbar);
    }
    
    private void prepareMenuComponentGroup(){
        btnNewCase = new NavigationButton("New Case");
        btnNewCase.addListener((Button.ClickListener) this);
        btnNewCase.setIcon(iconNewCase);
        btnNewCase.addStyleName("pill");

        btnTaskList = new NavigationButton("Inbox");
        btnTaskList.addListener((Button.ClickListener) this);
        btnTaskList.setIcon(iconTaskList);
        btnTaskList.addStyleName("pill");

        btnTaskArchive = new NavigationButton("Archive");
        btnTaskArchive.addListener((Button.ClickListener) this);
        btnTaskArchive.setIcon(iconTaskArchive);
        btnTaskArchive.addStyleName("pill");

        btnDashboard = new NavigationButton("Dashboard");
        btnDashboard.addListener((Button.ClickListener) this);
        btnDashboard.setIcon(iconDashboard);
        btnDashboard.addStyleName("pill");

        menuComponentGroup.addComponent(btnNewCase);
        menuComponentGroup.addComponent(btnTaskList);
        menuComponentGroup.addComponent(btnTaskArchive);
        menuComponentGroup.addComponent(btnDashboard);
    }

    public void refreshCounts() {
        btnNewCase.setDescription("" + getCasesCount());
        btnTaskList.setDescription("" + getTasklistCount());
        btnTaskArchive.setDescription("" + getTaskArchiveCount());
        btnDashboard.setDescription("2");
        root.addComponent(menuComponentGroup);
    }

    private int getCasesCount() {
        int result = 0;
        try {
            Collection<LightProcessDefinition> processes = TouchProcessApplication.getCurrent().getBpmModule().getLightProcessDefinitions(ProcessState.ENABLED);
            if (!processes.isEmpty()) {
                result = processes.size();
            }
        } catch (Exception ex) {
            Logger.getLogger(MainNavigationView.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    private int getTasklistCount() {
        int result = 0;
        try {
            Collection<LightTaskInstance> tasks = TouchProcessApplication.getCurrent().getBpmModule().getLightTaskList(ActivityState.READY);
            tasks.addAll(TouchProcessApplication.getCurrent().getBpmModule().getLightTaskList(ActivityState.EXECUTING));
            tasks.addAll(TouchProcessApplication.getCurrent().getBpmModule().getLightTaskList(ActivityState.SUSPENDED));
            if (!tasks.isEmpty()) {
                result = tasks.size();
            }
        } catch (Exception ex) {
            Logger.getLogger(MainNavigationView.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    private int getTaskArchiveCount() {
        int result = 0;
        try {
            Collection<LightTaskInstance> tasks = TouchProcessApplication.getCurrent().getBpmModule().getLightTaskList(ActivityState.FINISHED);
            if (!tasks.isEmpty()) {
                result = tasks.size();
            }
        } catch (Exception ex) {
            Logger.getLogger(MainNavigationView.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    private void prepareToolbar() {
        toolbar = new NavigationBar();

        btnRefresh = new Button(null, this);
        btnRefresh.setIcon(refreshIcon);
        btnRefresh.addStyleName("no-decoration");

        toolbar.setLeftComponent(btnRefresh);

        final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm:ss");
        toolbar.setCaption("Updated " + formatter.format(Calendar.getInstance().getTime()));

    }

    public void buttonClick(ClickEvent event) {
        if (event.getButton().equals(btnTaskList)) {
            TaskListNavigationView taskList = new TaskListNavigationView();
            TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getMenuNavigationManager().navigateTo(taskList);
            taskList.initUI();
            taskList.refreshTable();
        } else if (event.getButton().equals(btnNewCase)) {
            NewProcessesNavigationView newProcesses = new NewProcessesNavigationView();
            TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getMenuNavigationManager().navigateTo(newProcesses);
            newProcesses.initUI();
            newProcesses.refreshTable();
        } else if (event.getButton().equals(btnTaskArchive)) {
            TaskArchiveNavigationView taskArchiveView = new TaskArchiveNavigationView();
            TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getMenuNavigationManager().navigateTo(taskArchiveView);
            taskArchiveView.initUI();
            taskArchiveView.refreshTable();
        } else if (event.getButton().equals(btnDashboard)) {
            DashboardNavigationView dashboardNavigationView = new DashboardNavigationView();
            TouchProcessApplication.getCurrent().getMobileMainWindow().getMainView().getMenuNavigationManager().navigateTo(dashboardNavigationView);
            dashboardNavigationView.initUI();
        } else if (event.getButton().equals(btnRefresh)) {
            refreshCounts();
            final SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy hh:mm:ss");
            toolbar.setCaption("Updated " + formatter.format(Calendar.getInstance().getTime()));
        }
    }
}
